layui.use(['form'], function () {
    var form = layui.form;
    var $ = layui.$;
    var layer = layui.layer;

    // 表单验证
    form.verify({
      required: function (value, item) {
        if (!value) {
          return '该项为必填项';
        }
      }
    });
    form.on('submit(login)', function (data) {
      var username = data.field.username;
      var password = data.field.password;
      var rpassword = data.field.rpassword;
      var nickname = data.field.nickname;

      if (password !== rpassword) {
        layer.msg('两次密码不一致', { icon: 0 })
        return false;
      }

      axios.post('http://localhost:9000/users/register', data.field)
        .then(function (response) {
          if (response.data.code === 1) {
            layer.msg(response.data.message, { icon: 1 });
            location.href = './login.html'
          } else {
            layer.msg(response.data.message, { icon: 0 })
          }
        })
        .catch(function (error) {
          layer.msg('请求出错');
        });

      return false; // 阻止表单默认提交
    });
  });